package com.djakapm.swing.component;

import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;

public class Dropdown extends JComboBox{
	private static final long serialVersionUID = -8907180420172371518L;
	private List<Object> data;

	public Dropdown() {
		super();
	}
	
	public Dropdown(List<Object> data) {
		this.data = data;
		for(Object datum:data) {
			this.addItem(datum.toString());
		}
	}
	
	public Dropdown(Object[] data) {
		super(data);
		this.data = new ArrayList<Object>();
		for(Object datum:data) {
			this.data.add(datum.toString());
		}
	}
	
	public boolean containsValue(Object item) {
		return this.data.contains(item);
	}
	
	public Dropdown removeValue(Object item) {
		if(this.data != null && !this.data.isEmpty()) {
			this.data.remove(item);
			this.removeItem(item);
		}
		return this;
	}
	
	public Dropdown addValue(Object newItem) {
		if(this.data == null) {this.data = new ArrayList<Object>();}
		if(!this.containsValue(newItem)) {
			this.data.add(newItem);
			this.addItem(newItem);
		}
		return this;
	}
	
	public void clear(){
		this.data.clear();
		((DefaultComboBoxModel)this.getModel()).removeAllElements();
	}
	
}
